package com.hhsj.dynamicui.server;

import com.hhsj.androidserver.HttpServlet;
import com.hhsj.androidserver.WebApp;
import com.hhsj.dynamicui.server.server.WifiService;
import com.hhsj.dynamicui.server.servlet.admin.consts.AddConstsServlet;
import com.hhsj.dynamicui.server.servlet.admin.consts.AddSpecConstsServlet;
import com.hhsj.dynamicui.server.servlet.admin.consts.GetConstsServlet;
import com.hhsj.dynamicui.server.servlet.user.AdServlet;
import com.hhsj.dynamicui.server.servlet.user.MenuServlet;
import com.hhsj.dynamicui.server.servlet.user.ModuleServlet;
import com.hhsj.dynamicui.server.utils.ConstantUtil;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Enter {

    public static void main(String[] args) {
        String ip = args[0];
        String port = args[1];
        String user = args[2];
        String password = args[3];
        Enter enter = new Enter();
        enter.init(ip, port, user, password);
        enter.prepareService();
        enter.startLoop();
    }

    private void startLoop() {
        String ip = getLocalIpAddress();
        System.out.println("ip:port = " + ip + ":" + ConstantUtil.PORT);
        WifiService serviceV2 = new WifiService();
        serviceV2.startWaitDataThread();
    }


    private void init(String ip, String port, String user, String password) {
        new Thread(() -> dynamicUIconnectSQL(ip, port, user, password)).start();

        new Thread(() -> adConnectSQL(ip, port, user, password)).start();
    }

    private void prepareService() {
        //启动的服务
        WebApp.getInstance().init(() -> {
            List<Class<? extends HttpServlet>> servlets = new ArrayList<>();

            //模块服务
            servlets.add(ModuleServlet.class);
            //广告服务
            servlets.add(AdServlet.class);
            //功能入口服务
            servlets.add(MenuServlet.class);
            //获取常量服务
            servlets.add(GetConstsServlet.class);
            //添加常量服务
            servlets.add(AddConstsServlet.class);
            //添加视图熟悉常亮
            servlets.add(AddSpecConstsServlet.class);

            return servlets;
        });
    }

    private String getLocalIpAddress() {
        try {
            ArrayList<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces());
            for (NetworkInterface ni : nilist) {
                ArrayList<InetAddress> ialist = Collections.list(ni.getInetAddresses());
                for (InetAddress address : ialist) {
                    if (address instanceof Inet4Address &&
                            !address.isLoopbackAddress() &&
                            !address.isLinkLocalAddress()) {
                        return address.getHostAddress();
                    }
                }
            }
        } catch (SocketException ex) {
            System.out.println(ex.toString());
            return "";
        }
        return "";
    }

    private void dynamicUIconnectSQL(String ip, String port, String user, String password) {
        DynamicUIService.getInstance().init(ip, port, user, password);
    }

    private void adConnectSQL(String ip, String port, String user, String password) {
        AdJDBCService.getInstance().init(ip, port, user, password);
    }
}
